﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using M3G;

namespace Sample_002_Simple_Shader_ {
    class Program {
        static void Main (string[] args) {
            var g3d = Graphics3D.GetInstance ();
            g3d.BindTarget (null);

            var wld = new World ();
            var cam = new Camera ();
            wld.AddChild (cam);
            wld.SetActiveCamera (cam);

            var msh = new Mesh (1, 0);
            {
                var positions = new VertexArray (4, 3, ArrayType.Float);
                var colors    = new VertexArray (4, 3, ArrayType.Byte);
                positions.Set (0, 4, new float[]{ 0.5f,-0.5f, 0,
                                              0.5f, 0.5f, 0,
                                             -0.5f,-0.5f, 0, 
                                             -0.5f, 0.5f, 0});
                colors.Set (0, 4, new byte[]{255, 0, 0,
                                         0, 255, 0,
                                         0, 0, 255,
                                         255, 255, 255});
                var vbuf  = new VertexBuffer ();
                var scale = 1.0f;
                var bias  = new float[] { 0, 0, 0 };
                vbuf.SetPositions (positions, scale, bias);
                vbuf.SetAttribute ("Color", colors, false, true);
                msh.SetVertexBuffer (vbuf);

                var strips     = new int[] { 4 };
                var firstIndex = 0;
                var ibuf       = new IndexBuffer (PrimitiveType.Triangles, strips, firstIndex);
                msh.SetIndexBuffer (0, ibuf);

                var vertShader = new VertexShader ("simple.vert");
                var fragShader = new FragmentShader ("simple.frag");
                var shaderProg = new ShaderProgram (vertShader, fragShader);

                var unifs     = new ShaderUniforms ();
                var uniformColorID = unifs.AddUniform ("UniformColor", ShaderVariable.Vec3, 0);
                unifs.SetUniformValue (uniformColorID, 1.0f, 0.5f, 1.0f);

                var app = new ShaderAppearance ();
                app.SetShaderProgram (shaderProg);
                app.AddShaderUniforms (unifs);

                msh.SetShaderAppearance (0, app);

                wld.AddChild (msh);
            }


            g3d.Render (wld);
            g3d.ReleaseTarget ();
        }
    }
}
